﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; 
using System.Data;

namespace Common
{
    public class FlexiJson
    {
        public class FlexiRow
        {
            public string Id { get; set; }
            public string[] Cells { get; set; }

            public FlexiRow(string Id, params string[] Cells)
            {
                this.Id = Id;
                this.Cells = Cells;
            }

            public FlexiRow(params string[] Cells)
            {
                this.Id = Cells[0];
                this.Cells = Cells;
            }


            public override string ToString()
            {
                return string.Format(@"{{""id"":""{0}"",""cell"":[""{1}""]}}",
                    Id, string.Join(@""",""", Cells));
            }
        }
        public int Totle { get; set; }
        public int CurrentPage { get; set; }
        public List<FlexiRow> Rows { get; set; }
        //public string Data { get; set; }
        public string Title { get; set; }
        public string[] Header { get; set; }

        private FlexiJson() { this.Rows = new List<FlexiRow>(); }


        public static FlexiJson LoadRows(DataSet source)
        {
            FlexiJson fr = new FlexiJson();

            if (source != null && source.Tables.Count > 0)
            {
                List<string> listCols = new List<string>();
                foreach (DataColumn col in source.Tables[0].Columns)
                {
                    listCols.Add(col.ColumnName);
                }
                fr.Header = listCols.ToArray();
            }

            if (source.DataSetHasData())
            {
                foreach (DataRow row in source.Tables[0].Rows)
                {
                    List<string> lstValue = new List<string>();
                    foreach (DataColumn col in source.Tables[0].Columns)
                    {
                        lstValue.Add(row[col].GetString());
                    }

                    fr.Rows.Add(new FlexiRow(lstValue.ToArray()));
                }
            }
            return fr;
        }

        public static FlexiJson LoadRows<T>(IEnumerable<T> source)
        {
            FlexiJson fr = new FlexiJson();

            bool IsFirst = true;
            List<string> lstHeader = new List<string>();
            foreach (T item in source)
            {
                List<string> lstValue = new List<string>();
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(item))
                {
                    if (IsFirst)
                    {
                        lstHeader.Add(descriptor.Name);
                    }

                    object obj2 = descriptor.GetValue(item);

                    lstValue.Add(obj2.GetString());
                }
                fr.Rows.Add(new FlexiRow(lstValue.ToArray()));

                if (IsFirst) IsFirst = false;
            }

            fr.Header = lstHeader.ToArray();
            return fr;
        }

        //public FlexiJson(int totle, int currentPage, params FlexiRow[] rows)
        //{
        //    this.Totle = totle;
        //    this.CurrentPage = currentPage;
        //    this.Rows = rows.ToList();
        //}

        public override string ToString()
        {
            return string.Format(@"{{""total"":{0},""page"":{1},""rows"":[{2}],""title"":""{3}"",""header"":{4}}}",
                    Totle,
                    CurrentPage,
                    string.Join(@",", Rows.Select(o => o.ToString()).ToArray()),
                    Title,
                    this.Header.ToJson());
        }
    }
}
